package com.bjpowernode.stusys.service.impl;

import com.bjpowernode.stusys.entity.Admin;
import com.bjpowernode.stusys.exception.SysException;
import com.bjpowernode.stusys.mapper.AdminDao;
import com.bjpowernode.stusys.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author jy
 * @version 1.0
 * @date 2021/12/1 10:17
 */
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminDao adminDao;

    /**
     * 注册功能的实现方法
     * 业务逻辑：
     * 1.判断用户输入的用户名是否存在
     *      1.1 如果存在
     *             注册失败
     *      1.2 如果不存在
     *              注册成功
     *  2.将用户信息写入数据库
     */
    @Override
    public void register(Admin admin) {
        Admin a = adminDao.findAdminByAccount(admin.getAccount());
        if (a != null){
            throw new SysException("用户名已存在");
        }

        int result = adminDao.insertAdmin(admin);
        if (result != 1){
            throw new SysException("注册失败");
        }
    }

    @Override
    public void login(Admin admin) {
        int i = adminDao.login(admin);
        if (i != 1){
            throw new SysException("登录失败");
        }
    }
}
